package com.shuai.tool;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MyTime {
    public static String getCurrentTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }
    public static String getCurrentTimeOfStamp(){
        String time = getCurrentTime();
        return toTimeStampForString(time);
    }
    public static String dayStrToTimeStamp(String dayStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        Date date;
        String time = null;
        try {
            date = sdf.parse(dayStr);
            long l = date.getTime();
            String stf = String.valueOf(l);
            time = stf.substring(0,10);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
    public static int dayStrToTimeStampForInt(String dayStr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        Date date = null;
        try {
            date = sdf.parse(dayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int) (date.getTime()/1000);
    }
    public static String toTimeStampForString(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdf.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0,10);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return times;
    }
    public static int toTimeStampForInt(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd",Locale.CHINA);
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int) (date.getTime()/1000);
    }
    public static String toDateString(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        int i = Integer.parseInt(time);
        return sdf.format(new Date(i*1000L));
    }
    public static int getYear(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        int i = Integer.parseInt(time);
        return Integer.valueOf(sdf.format(new Date(i*1000L)));
    }
    public static int getMonth(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        int i = Integer.parseInt(time);
        return Integer.valueOf(sdf.format(new Date(i*1000L)));
    }
    public static int getDay(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        int i = Integer.parseInt(time);
        return Integer.valueOf(sdf.format(new Date(i*1000L)));
    }
    public static boolean isTheSameDay(int day1, int day2){
        return Math.abs(day1 - day2) < 86400 ? true : false;
    }
    public static boolean isDayBefortCurrentDay(String day){
        return (Integer.valueOf(day) - Integer.valueOf(getCurrentTimeOfStamp())) < -86400;
    }
}
